require.config({
    paths: {
        'new-echarts': '../addons/qingdongams/echarts.min',
    },
    shim: {
        'new-echarts': {
            deps: ['jquery'],
            exports: '$.fn.extend'
        },
    }
});
define(['jquery', 'bootstrap', 'backend', 'addtabs', 'table', 'new-echarts',  'template','form'],
    function ($, undefined, Backend, Datatable, Table, NewEcharts, Template,Form) {

        var Controller = {
            //合同数量统计分析
            index: function () {
                //客户
                console.log(Config);
                $.each(Config.echarts,function (index,item){
                    var myChart2=NewEcharts.init(document.getElementById('x_'+index), 'walden');
                    var option = {
                        borderWidth: 0,
                        shadowColor: '#fff',
                        shadowBlur: 2,

                        xAxis: {
                            show: false,
                            boundaryGap: false,
                            type: 'category',
                            data: [1, 2, 3, 4, 5, 6]
                        },
                        yAxis: {
                            show: false,
                            type: 'value',
                        },
                        series: [
                            {
                                data: item,
                                type: 'line',
                                symbol: 'none',
                                areaStyle: {},
                                smooth: true,

                            }
                        ]
                    };
                    if(index == 'customer'){
                        option.color = [
                            '#fff',
                        ];
                        option.shadowColor = '#fff';
                    }else{
                        option.color = [
                            '#0052cc',
                        ];
                        option.shadowColor = '#0052cc';
                    }
                    myChart2.setOption(option);
                });
                //业绩指标完成率
                var achievementCharts=NewEcharts.init(document.getElementById('x_achievement'));
                var achievementOption = {
                    tooltip: {
                        borderWidth: 1,
                        formatter: '{a} <br/>{b} : {c}%'
                    },
                    series: [
                        {
                            name: '业绩指标完成率',
                            type: 'gauge',
                            progress: {
                                show: true,
                            },
                            detail: {
                                valueAnimation: true,
                                formatter: '{value}%'
                            },
                            data: [
                                {
                                    value: 50,
                                    name: '完成率'
                                }
                            ]
                        }
                    ]
                };

                $('#achievement_status').on('change',function (){
                    var status=$('#achievement_status').val();
                    var serialize=$('#form').serialize();
                    serialize=serialize+'&status='+status
                    $.post('qingdongams/dashboard/get_achievement_detail', serialize, function (data){
                        if(data.code == 1){
                            if(status == 3){
                                $('.completeMoney').html(data.data.contractMoney)
                            }else{
                                $('.completeMoney').html(data.data.receivablesMoney)
                            }
                            if(data.data.achievementMoney == 0){
                                $('.setting').hide();
                                $('.nosetting').show();
                            }else{
                                $('.setting').show();
                                $('.nosetting').hide();
                            }
                            $('.achievementMoney').html(data.data.achievementMoney)
                            achievementOption.series[0].data[0].value=data.data.rate;
                            achievementCharts.setOption(achievementOption);
                        }
                    },'json');
                }).trigger('change');
                //业绩目标
                var achievementMoney=NewEcharts.init(document.getElementById('x_achievement_money'));
                var achievementMoneyOption = {

                    tooltip: {
                        trigger: 'axis'
                    },
                    color:[
                        '#000',
                        '#0052cc'
                    ],
                    grid: {
                        left: '3%',
                        right: '4%',
                        bottom: '3%',
                        containLabel: true
                    },
                    toolbox: {
                        feature: {
                            saveAsImage: {}
                        }
                    },
                    xAxis: {
                        type: 'category',
                        boundaryGap: false,
                        data: [],
                    },
                    yAxis: {
                        type: 'value',

                    },
                    series: [
                        {
                            name: '目标金额',
                            type: 'line',
                            stack: 'Total',
                            data: []
                        },
                        {
                            name: '实际完成金额',
                            type: 'line',
                            stack: 'Total',
                            data: []
                        },

                    ]
                };

                $('#achievement_status2').on('change',function (){
                    var status=$('#achievement_status2').val();
                    var serialize=$('#form').serialize();
                    serialize=serialize+'&status='+status
                    if(status == 3){
                        $("#achievement_title").html("订单金额目标及完成情况");
                    }else{
                        $("#achievement_title").html("回款金额目标及完成情况");
                    }
                    $.post('qingdongams/dashboard/get_achievement', serialize, function (data){
                        if(data.code == 1){
                            achievementMoneyOption.xAxis.data=data.data.times;
                            achievementMoneyOption.series[0].data=data.data.achievement;
                            achievementMoneyOption.series[1].data=data.data.money;
                            achievementMoney.setOption(achievementMoneyOption);
                        }
                    },'json');
                }).trigger('change');


                // 销售漏斗
                var businessL=NewEcharts.init(document.getElementById('x_business_l'));
                var businessLOption = {
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {
                            type: 'shadow'
                        }
                    },
                    legend: {},
                    grid: {
                        left: '3%',
                        right: '4%',
                        bottom: '3%',
                        containLabel: true
                    },
                    xAxis: {
                        type: 'value',
                    },
                    yAxis: {
                        type: 'category',
                        data: ['赢单','输单']
                    },

                    series: [
                        {
                            type: 'bar',
                            data: []
                        },
                    ]
                };
                $('.wk-toggle-item').on('click',function (){
                    var serialize=$('#form').serialize();
                    if($(this).data('value') == 1){
                        $.post('qingdongams/dashboard/get_business', serialize, function (data){
                            if(data.code == 1){
                                businessLOption.series[0].data=[data.data.contractBusinessMoney,data.data.noBusinessMoney];
                                businessL.setOption(businessLOption);
                            }
                        },'json');
                    }else{
                        $.post('qingdongams/dashboard/get_business', serialize, function (data){
                            if(data.code == 1){
                                businessLOption.series[0].data=[data.data.contractBusiness,data.data.noBusiness];
                                businessL.setOption(businessLOption);
                            }
                        },'json');
                    }
                    $('.wk-toggle-item').removeClass('selected');
                    $(this).addClass('selected');
                });
                $('.wk-toggle-item.selected').trigger('click');


                $('#ranking').on('change', function () {
                    var ranking = $('#ranking').val();

                    var serialize = $('#form').serialize();
                    serialize = serialize + '&ranking=' + ranking
                    var table = $("#table"+ranking),columns;
                    if(ranking == 1){
                        columns= [
                            [
                                {field: 'ranking', title: __('排名'), operate: false},
                                {field: 'staff.name', title: __('姓名'), operate: false},
                                {field: 'number', title: __('新增客户数（个）'), operate: false},
                            ]
                        ];
                    }else if(ranking == 2){
                        columns= [
                            [
                                {field: 'ranking', title: __('排名'), operate: false},
                                {field: 'staff.name', title: __('姓名'), operate: false},
                                {field: 'number', title: __('新增跟进记录数（条）'), operate: false},
                            ]
                        ];
                    }else if(ranking == 3){
                        columns= [
                            [
                                {field: 'ranking', title: __('排名'), operate: false},
                                {field: 'staff.name', title: __('姓名'), operate: false},
                                {field: 'number', title: __('合同金额（元）'), operate: false},
                                {field: 'rate', title: __('目标完成率（%）'), operate: false},
                            ]
                        ];
                    }else if(ranking == 4){
                        columns= [
                            [
                                {field: 'ranking', title: __('排名'), operate: false},
                                {field: 'staff.name', title: __('姓名'), operate: false},
                                {field: 'number', title: __('回款金额（元）'), operate: false},
                                {field: 'rate', title: __('目标完成率（%）'), operate: false},
                            ]
                        ];
                    }else if(ranking == 5){
                        columns= [
                            [
                                {field: 'ranking', title: __('排名'), operate: false},
                                {field: 'staff.name', title: __('姓名'), operate: false},
                                {field: 'number', title: __('工单数（个）'), operate: false},
                            ]
                        ];
                    }else if(ranking == 6){
                        columns= [
                            [
                                {field: 'ranking', title: __('排名'), operate: false},
                                {field: 'staff.name', title: __('姓名'), operate: false},
                                {field: 'number', title: __('合同数（个）'), operate: false},
                            ]
                        ];
                    }
                    var option = {
                        url: 'qingdongams/dashboard/ranking?' + serialize,
                        sortName: 'id',
                        columns: columns,
                        //启用普通表单搜索
                        search: false,
                        commonSearch: false,
                        searchFormVisible: false,
                    };
                    // 初始化表格
                    table.bootstrapTable(option);
                    Table.api.bindevent(table);
                    // 为表格绑定事件
                    $('.table').hide()
                    $('#table'+ranking).show()
                }).trigger('change');

                $('#times').on('change',function (){
                    if($(this).val() == 'else'){
                        $('#dates').show()
                    }else{
                        $('#form').submit();
                    }
                })
                $('input[name="dates"]').on('blur',function (){
                    console.log('11')
                    $('#form').submit();
                })
                $('select[name="type"]').on('change',function (){
                    $('#form').submit();
                })

                $('#myPopover').popover();
                Form.events.selectpage($("form"));
                Form.events.daterangepicker($("form"));
            },
        };

        return Controller;
    });
